/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.menu.file;

import com.thoughtworks.xstream.XStream;
import data.classDiagram.*;
import data.DataVector;
import data.Position;
import data.stateDiagram.ConditionalTransition;
import data.stateDiagram.State;
import data.stateDiagram.Transition;
import gui.commands.CommandManager;
import gui.menu.edit.NewItem;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenuEntryPrimary;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Student
 */
public class ImportFromXmlItem extends RibbonApplicationMenuEntryPrimary implements ActionListener {

	private final JFileChooser fc = new JFileChooser();

	public ImportFromXmlItem() {
		super(ImageWrapperResizableIcon.getIcon(
				ClassLoader.getSystemClassLoader().getResource("icons/open.png"),
				new Dimension(16, 16)), "Open", null, JCommandButton.CommandButtonKind.ACTION_ONLY);

		this.mainActionListener = this;

        fc.setFileFilter(new FileNameExtensionFilter("Class diagram editor files (*.cde)", "cde"));
        fc.setAcceptAllFileFilterUsed(false);
    }

    public void actionPerformed(ActionEvent e) {
	    if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(null,
			    "Any unsaved changes will be lost. Are you sure you want open new project?"))
		    return;

	    CommandManager.getInstance().clearHistory();
	    DataVector.getInstance().clear();

        int returnVal = fc.showOpenDialog(null);
        FileReader reader = null;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (!file.exists()) {
            } else {
                try {
                    XStream xstream = new XStream();
                    xstream.processAnnotations(new Class[]{Entity.class,
                            Member.class, ClassEntity.class,
                            Method.class, MethodArgument.class,
                            Position.class, InterfaceEntity.class,
                            Trigger.class, ConditionalTransition.class,
                            Transition.class, State.class
                    });

                    reader = new FileReader(file);
                    ObjectInputStream in = xstream.createObjectInputStream(reader);
                    DataVector.getInstance().clear();

                    while (true) {
                        try {
                            Entity entity = (Entity) in.readObject();
                            //entity.afterUnmarshaling();

                            DataVector.getInstance().addEntity(entity);
                        } catch (Exception ex) {
                            break;
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
